﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HKEA.CMS.Core.BLL;
using HKEA.CMS.Core.Models;
using HKEA.CMS.Core.Utility;
using HKEA.CMS.Core.BLL.Tbl;

namespace WebApplication
{
    public partial class M_ChangePassword : BasePage
    {
        tblCust_WebLogin ent = new tblCust_WebLogin();
        tblCust_WebLoginBLL bll = new tblCust_WebLoginBLL();
        static int MemberUID;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (GetMemberLoginUID() > 0)
                {
                    MemberUID = GetMemberLoginUID();
                }
                else {
                    SetNoOption();
                    usermeg.Visible = false;
                    err.Text = ResourceManage.GetResourceString("msg200002");
                }
            }
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (MemberUID > 0 )
            {
                ent.CustWebLoginUID = MemberUID;
                if (ent != null && !string.IsNullOrEmpty(OldPassword.Text) && !string.IsNullOrEmpty(NewPassword.Text))
                {
                    if (che(NewPassword.Text.Length))
                    {
                        foreach (tblCust_WebLogin item in bll.GetListByENT(ent))
                        {
                            if (OldPassword.Text == item.LoginPassword)
                            {
                                ent.CustWebLoginUID = item.CustWebLoginUID;
                                ent.CustomerUID = item.CustomerUID;
                                ent.LoginName = item.LoginName;
                                ent.Status = item.Status;
                                ent.LoginPassword = NewPassword.Text;
                                ent.LastModifyBy = Session[Constant.SessionName.Manage] == null ? "system" : (Session[Constant.SessionName.Manage] as tblUser).User_Name.Trim();
                                bll.UpdateENT(ent);
                                err.Visible = true;
                                err.Text = ResourceManage.GetResourceString("msg100005");
                                WriteToLog();
                            }
                            else
                            {
                                err.Text = ResourceManage.GetResourceString("msg400001");
                            }
                            ResetControl();
                        }
                    }
                    else {
                        err.Text = ResourceManage.GetResourceString("msg400005");
                    }
                }
                else
                {
                    err.Text = ""; 
                }
            }
            else
            {
                err.Text = ResourceManage.GetResourceString("msg400004");
            }             
        }
        void ResetControl() {
            OldPassword.Text = string.Empty;
            NewPassword.Text = string.Empty;
            ComfirmWord.Text = string.Empty;
        }
        #region 检查输入的字符串是否符合需求
        bool che(int i)
        {
            if (i < 6)
            {
                return false;
            }
            else if (i > 16)
            {
                return false;
            }
            else
            {
                return true;
            }
        }
        #endregion        

        protected void Quit_Click(object sender, EventArgs e)
        {
            NewPassword.Text = "";
            OldPassword.Text = "";
            ComfirmWord.Text = "";
            err.Text = "";
        }
        private void SetNoOption()
        {
            lkBtnSave.Enabled = false;
            err.Text = ResourceManage.GetResourceString("msg200001");
        }

        protected void WriteToLog()
        {
            string LoginName = tblCust_WebLoginBLL.GetInstance().GetENT(GetMemberLoginUID()).LoginName.ToString();
            tblHistory entlog = new tblHistory();
            Literal CompanyName = usermeg.FindControl("ltlCompanyName_eng") as Literal;
            entlog.HistoryCatUID = 1;
            entlog.HistoryTypeUID = 2;
            entlog.CustomerUID = GetEnt();
            entlog.Cust_MemberUID = 0;
            entlog.ContactPersonUID = 0;
            entlog.History_Detail = LoginName + " {"+CompanyName.Text+"}{WebLogin}have Change password";
            entlog.LastModifyBy = LoginName;
            int tal = tblHistoryBLL.GetInstance().InsertENT(entlog);
        }
    }
}